#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class ModuleListProvider : CY.GFive.Core.Providers.DALProvider.IModuleListProvider
	{
		#region Sql
		private static readonly string SqlSelectModuleList = "SELECT [Id], [ModuleName],[ParentModuleKey] FROM [ModuleList] WHERE  [Id]=@Id";

		private static readonly string SqlInsertModuleList = "INSERT INTO [ModuleList]([ModuleName],[ParentModuleKey]) values(@ModuleName,@ParentModuleKey);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateModuleList = "UPDATE [ModuleList] SET [ModuleName] = @ModuleName,[ParentModuleKey] = @ParentModuleKey WHERE  [Id]=@Id";

		private static readonly string SqlDeleteModuleList = "DELETE [ModuleList] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllModuleList = "SELECT [Id], [ModuleName],[ParentModuleKey] FROM [ModuleList]";
		#endregion

		#region IModuleListProvider Members
		public Core.Business.ModuleList Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectModuleList);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.ModuleList moduleList = new Core.Business.ModuleList();

				if (!reader.IsDBNull(0)) moduleList.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) moduleList.ModuleName = reader.GetString(1);
				if (!reader.IsDBNull(2)) moduleList.ParentModuleKey = reader.GetInt32(2);

				return moduleList;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.ModuleList moduleList)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@ModuleName", SqlDbType.NVarChar, moduleList.ModuleName);
			sql.AddParameter("@ParentModuleKey", SqlDbType.Int, moduleList.ParentModuleKey);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertModuleList);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) moduleList.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.ModuleList moduleList)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@ModuleName", SqlDbType.NVarChar, moduleList.ModuleName);
			sql.AddParameter("@ParentModuleKey", SqlDbType.Int, moduleList.ParentModuleKey);
			sql.AddParameter("@Id", SqlDbType.Int, moduleList.Id);
			sql.ExecuteSql(SqlUpdateModuleList);
		}

		public void Delete(Core.Business.ModuleList moduleList)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, moduleList.Id);
			sql.ExecuteSql(SqlDeleteModuleList);
		}

		public IList<Core.Business.ModuleList> GetAllModuleList()
		{
			IList<Core.Business.ModuleList> moduleListlist = new List<Core.Business.ModuleList>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllModuleList);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.ModuleList moduleList = new Core.Business.ModuleList();

					if (!reader.IsDBNull(0)) moduleList.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) moduleList.ModuleName = reader.GetString(1);
					if (!reader.IsDBNull(2)) moduleList.ParentModuleKey = reader.GetInt32(2);

					moduleList.MarkOld();
					moduleListlist.Add(moduleList);
				}
				reader.Close();
			}
			return moduleListlist;
		}
		#endregion
	}
}
